<template>
  <div>
    <!-- 面包屑导航区-->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
<!--    卡片区域-->
    <el-card>
<!--      添加分类区域-->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateList">添加分类</el-button>
        </el-col>
      </el-row>
<!--      分类列表区-->
    <tree-table
      :data="cateList"
      :columns="columns"
      border
      :selection-type="false"
      :expand-type="false"
      show-index>
      <template slot="isok" slot-scope="scope">
        <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color: green"></i>
        <i class="el-icon-error" v-else style="color: red"></i>
      </template>
      <template slot="order" slot-scope="scope">
        <el-tag type="primary" v-if="scope.row.cat_level === 0 ">一级</el-tag>
        <el-tag type="warning" v-else-if="scope.row.cat_level === 1">二级</el-tag>
        <el-tag type="danger" v-else>三级</el-tag>
      </template>
      <template slot="opt">
        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
      </template>
    </tree-table>
<!--      分页功能-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
<!--      添加分类对话框-->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateDialogVisible"
        width="50%">
        <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
          <el-form-item label="添加分类:" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              :options="parentCateList"
              :props="cascaderProps"
              v-model="selectedKeys"
              clearable
              @change="parentCateChanged">
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      // 定义查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //获取的分类数据列表
      cateList: [],
      total: 0, // 总分类条数,
      columns: [
        { label:'分类名称',
          prop:'cat_name'
        },
        { label:'是否有效',
          type: 'template',
          template: 'isok'
        },
        { label:'排序',
          type: 'template',
          template: 'order'
        },
        { label:'操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialogVisible: false, //添加分类对话框,
      addCateForm: {
        //分类名称
        cat_name: '',
        //默认添加1级分类
        cat_pid: 0,
        cat_level: 0
      }, // 添加分类表单,
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      parentCateList: [], //父级分类
      //相关参数
      cascaderProps:{
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true // 是否遵守父子组件之间不关联
      },
      selectedKeys: [] //用来存储被选中的分类的id
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    async getCateList() {
      let { data: res } = await this.$http.get(`categories`, {
        params: this.queryInfo
      })
      // console.log(res.data)
      if (res.meta.status !== 200) return this.$message.error(res.msg)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    //分页功能实现
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    },
    //添加分类显示对话框
    showAddCateList() {
      // 调用获取父级分类
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    //点击确定,添加分类
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        //验证通过,发送请求
        let { data: res } = await this.$http.post(`categories`, this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('获取分类失败')
        this.addCateDialogVisible = false
        this.getCateList()
      })
    },
    //获取父级分类数据
    async getParentCateList () {
      let { data: res } = await this.$http.get(`categories`, {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类失败')
      this.parentCateList = res.data
      // console.log(this.parentCateList)
    },
    // 级联选择器发生改变是调用
    parentCateChanged() {
      // console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_level = this.selectedKeys.length
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    }
  }
}
</script>

<style scoped lang="less">
  .el-row{
    margin-bottom: 20px;
  }
  .el-cascader{
    width: 100%;
  }
</style>
